package com.noble.glamour.third.auth;

import android.support.v4.app.FragmentActivity;

import com.noble.glamour.bean.start.LoginResult;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.TimerUtil;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.third.ThirdConstant;
import com.noble.library.log.PLogger;
import com.noble.library.log.PToast;
import com.noble.library.third.auth.OauthCallback;
import com.noble.library.third.auth.ThirdInfo;

/**
 * 三方结果回调
 * Created by Su on 2017/10/13.
 */
public class OauthBack implements OauthCallback {
    private FragmentActivity activity;

    public OauthBack(FragmentActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onSuccess(int platform, final ThirdInfo user) {
        PLogger.d("Third---onSuccess: " + platform + " " + user);

        LoadingDialog.show(activity, "正在登录");
        final String openid = user.getOpenid();
        ModuleMgr.getLoginMgr().weChatLogin(openid, new RequestComplete() {
            @Override
            public void onRequestComplete(final HttpResponse response) {
                LoadingDialog.closeLoadingDialog(200, new TimerUtil.CallBack() {
                    @Override
                    public void call() {
                        PToast.showShort(response.getMsg());
                        if (!response.isOk()) return;

                        LoginResult data = (LoginResult) response.getBaseData();
                        ModuleMgr.getLoginMgr().setCookie(data.getAuth());
                        ModuleMgr.getLoginMgr().putAllLoginInfo(data.getUid(), "", true);

                        if (data.isNew()) {   // 新用户
                            UIShow.showRegInfoAct((FragmentActivity) App.activity, ThirdConstant.WECHAT, openid);
                            return;
                        }

                        // 老用户： 去请求个人资料
                        ModuleMgr.getCenterMgr().reqMyInfo(new RequestComplete() {
                            @Override
                            public void onRequestComplete(HttpResponse response) {
                                UIShow.showMainClearTask(App.activity);
                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    public void onFailure(int platform, String msg) {
        PLogger.d("Third----onFailure: " + platform + " " + msg);
        LoadingDialog.closeLoadingDialog();
        PToast.showShort("登录失败");
    }

    @Override
    public void onCancelled(int platform) {
        PLogger.d("Third----取消登录: " + platform);
        LoadingDialog.closeLoadingDialog();
    }

    @Override
    public void onError(int platform, Exception ex) {
        PLogger.d("Third----onError: msg: " + platform);
        LoadingDialog.closeLoadingDialog();
        PToast.showShort("登录失败");
    }
}
